package com.yuntianshi.controller;


import com.yuntianshi.common.*;
import com.yuntianshi.config.MinioConfig;
import com.yuntianshi.pojo.Setmeal;
import com.yuntianshi.service.SetmealService;
import com.yuntianshi.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//import redis.clients.jedis.JedisPool;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

//    使用JedisPool操作Redis服务
//    @Autowired
//    private JedisPool jedisPool;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioConfig prop;

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try{
            //判断桶是否存在
            if (!minioUtils.bucketExists(prop.getBucketName2())){
                //桶不存在，创建桶
                minioUtils.makeBucket(prop.getBucketName2());
            }
            String fileName = minioUtils.upload(imgFile,prop.getBucketName2());
            //图片上传成功
            Result result = new Result(true, MessageContant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
//            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            //图片上传失败
            return new Result(false,MessageContant.PIC_UPLOAD_FAIL);
        }
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            if (!(checkgroupIds!=null&&checkgroupIds.length>0)){
                return new Result(false,"添加套餐失败，内容组不能为空");
            }
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false,MessageContant.ADD_SETMEAL_FAIL);
        }
        //新增套餐成功
        return new Result(true,MessageContant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //获取所有套餐信息
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        try{
            List<Setmeal> list = setmealService.findAll();
            return new Result(true, MessageContant.GET_SETMEAL_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageContant.GET_SETMEAL_LIST_FAIL);
        }
    }

    //根据id查询套餐信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageContant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageContant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){

        try {
            List<Integer> checkGroupIds=setmealService.findCheckGroupIdsBySetmealId(id);
            return new Result(true,MessageContant.QUERY_CHECKITEM_SUCCESS,checkGroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageContant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
        }catch (Exception e){
            return new Result(false,MessageContant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true,MessageContant.EDIT_SETMEAL_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            setmealService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false,MessageContant.DELETE_SETMEAL_FUIL);
        }
        return new Result(true,MessageContant.DELETE_SETMEAL_SUCCESS);
    }

}
